home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13423 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++,comp.object,comp.object.logic
  4. Subject: Re: polymorphism
  5. Date: Mon, 25 Mar 1996 12:56:07 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <3156DE37.4782@datalytics.com>
  8. References: <4j2k2b$4pj@arl-news-svc-3.compuserve.com>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. .,@compuserve.com wrote:
  16. >  >>What is polymorphism.
  17. >   The usual example is a shape object.
  18. >  Shape is your base class.
  19. >   From this you derive the classes called Circle, Triangle, and Square.
  20. > Shape has a function called Draw()
  21. > So each of the 3 classes (Circle, Triangle, Square) redo the Draw() function
  22. > in there own way.  Thus they all will be drawn differently from the others.
  23. >   The compiler can handle this.  The classes are said to be able
  24. > to draw themselves.
  25. >   Note the Draw() must be identicle to the Shape classes Draw().
  26. >   You can't change the argument list, this would be an overloaded
  27. > function and would not be the same function.
  28.  
  29. This behavior only applies to virtual mfs.  Here's an example to 
  30. illustrate the point:
  31.  
  32.     Shape* pShape = 0;
  33.     switch (shapeToDraw)
  34.     {
  35.         case CIRCLE:
  36.         {
  37.             pShape = new Circle;
  38.         }
  39.         break;
  40.         case TRIANGLE:
  41.         {
  42.             pShape = new Triangle;
  43.         }
  44.         break;
  45.         case SQUARE:
  46.         {
  47.             pShape = new Square;
  48.         }
  49.         break;
  50.     }
  51.     if (pShape)
  52.     {
  53.         pShape->Draw();
  54.     }
  55.  
  56. As you can see, this code uses a pointer to the base class 
  57. (Shape) to invoke the Draw mf.  The implementation invoked 
  58. depends upon the type of the object to which pShape points.  If 
  59. shapeToDraw had the value CIRCLE, pShape->Draw() would invoke 
  60. Circle::Draw().
  61.  
  62. BTW, this also applies to references, except you can only 
  63. initialize references.  Therefore, the polymorphic behavior 
  64. through a reference is normally limited to occassions in which a 
  65. function has a reference to the base class.  You can pass 
  66. derived class objects to the function, invoking the correct 
  67. derived class' virtual functions through that reference.
  68.  
  69. -- 
  70. Robert Stewart        | My opinions are usually my own.
  71. Datalytics, Inc.    | stew@datalytics.com
  72.